import React, { Component } from 'react';
import PropTypes from 'prop-types';
import store from '../Store';
export  default class Summary extends Component{
    constructor(props){
        super(props);
        this.onChange = this.onChange.bind(this);
        this.state = this.getOwnState();
    }
    getOwnState(){
        const state= store.getState();
        let sum= 0;
        for(const key in state){
            sum = state[key]+sum;
        }
        return{
            sum:sum
        }
    }
    componentDidMount(){
        store.subscribe(this.onChange)
    }
    componentWillUnmount(){
        store.unsubscribe(this.onChange)
    }
    onChange(){
        this.setState(this.getOwnState())
    }
    render(){
        const {caption}= this.props;
        const buttonStyle={
            border:'1px solid #ccc',
            marginRight:'5px',
            padding:'0 10px'
        };
        return (
            <div className="e-mb10">
                total:{this.state.sum}
            </div>
        )
    }
}